package com.ruoyi.doc.controller;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.doc.domain.Company;
import com.ruoyi.doc.domain.ContentType;
import com.ruoyi.doc.service.ICompanyService;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.service.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doc/company")
public class CompanyController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ICompanyService companyService;

    /**
     * 获取部门下拉文档树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysDept dept) {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return AjaxResult.success(companyService.buildDeptTreeSelect(depts));
    }

    @GetMapping("/lazyNode/{parentNodeId}")
    public AjaxResult lazyNode(@PathVariable("parentNodeId") Long parentNodeId) {
        //查询当前机构下的
        List<SysDept> depts = companyService.selectLazyDept(parentNodeId);
        return AjaxResult.success(companyService.buildDeptTreeSelect(depts));
    }

    /**
     * 获取文档列表
     */
    @PreAuthorize("@ss.hasPermi('doc:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(Company company) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = "";
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        }
        Map<String, Object> returnMap = companyService.selectCompanyList(company, pageNum, pageSize, orderBy);
        TableDataInfo rspData = new TableDataInfo();
        Map<String, Object> param = new HashMap<>();
        param.put("serverUrl", serverConfig.getUrl());
        rspData.setParam(param);
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        if (!ServiceUtil.isSuccess(returnMap)) {
            rspData.setRows(Collections.emptyList());
            rspData.setTotal(0);
            return rspData;
        }
        List<ContentType> list = UtilGenerics.checkList(returnMap.get("rows"));
        Integer total = UtilMisc.toInteger(returnMap.get("total"));
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }
}
